package com.gcb.service.impl;

import com.gcb.entity.Account;
import com.gcb.mapper.AccountMapper;
import com.gcb.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountMapper accountMapper;

    @Transactional
    public void transferMoney(int AId, int BId, double money) {
        Account accountA = new Account(AId, money);
        Account accountB = new Account(BId, money);
        double balance = getById(AId).getMoney();
        if (balance >= money) {
            int result1 = accountMapper.subMoney(accountA);
            int result2 = accountMapper.addMoney(accountB);
            if (result1 != 0 && result2 != 0) {
                System.out.println("转账成功");
            }
        } else {
            System.out.println("余额不足");
        }
    }

    @Override
    public List<Account> listAll() {
        return accountMapper.listAll();
    }

    @Override
    public Account getById(Integer id) {
        return accountMapper.getById(id);
    }
}
